package com.nuoniu.sibanyun.controller.erp;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.erp.ErpCurrency;
import com.nuoniu.sibanyun.entity.erp.ErpCurrencyAccount;
import com.nuoniu.sibanyun.service.erp.IErpCurrencyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  币种前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/erp/currency")
@Api(tags = "币种设置管理")
public class ErpCurrencyController extends BaseController {

    @Autowired
    private IErpCurrencyService currencyService;

    @GetMapping("pageList")
    @ApiOperation("币种列表")
    public HttpResult pageList(Page page, ErpCurrency currency) {
        Page<ErpCurrency> currencyPage = currencyService.pageList(page, currency);
        return HttpResult.ok(currencyPage);
    }

    @GetMapping("getDetailList")
    @ApiOperation("明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currencyId", value = "币种id"),
            @ApiImplicitParam(name = "year", value = "年份"),
            @ApiImplicitParam(name = "month", value = "月份")
    })
    public HttpResult<ErpCurrencyAccount> getDetailList(Integer currencyId, Integer year, Integer month ) {
        return HttpResult.ok(currencyService.getDetailList(currencyId, year, month));
    }

    @GetMapping("getAll")
    @ApiOperation("获取所有")
    public HttpResult getAll(Boolean isBaseCurrency) {
        return HttpResult.ok(currencyService.getAll(isBaseCurrency));
    }

    @PostMapping("save")
    @ApiOperation("保存币种设置")
    public HttpResult save(@RequestBody ErpCurrency currency) {
        if(StringUtils.isBlank(currency.getName())) {
            throw new NuoNiuException("币名不能为空");
        }
        if(StringUtils.isBlank(currency.getCode())) {
            throw new NuoNiuException("币符不能为空");
        }
        if(!currency.getIsBaseCurrency() && currency.getExChangeRateType() == null) {
            throw new NuoNiuException("汇率类型不能为空");
        }
        if(!currency.getIsBaseCurrency() && (currency.getOrderList() == null || currency.getOrderList().size() == 0)) {
            throw new NuoNiuException("明细不能空");
        }

        currencyService.saveCurrency(currency);
        return HttpResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("删除币种")
    public HttpResult delete(@RequestBody List<Long> ids) {
        currencyService.deleteByIds(ids);
        return HttpResult.ok();
    }

    @ApiOperation("获取币种详情")
    @GetMapping("getCurrencyDetail")
    public HttpResult getCurrencyDetail(Long id) {
        return HttpResult.ok(currencyService.getCurrencyDetail(id));
    }
}
